package com.jmyd.commons.base.support;

import com.jmyd.commons.constants.Constant;
import com.jmyd.commons.constants.JwtConstant;
import com.jmyd.commons.domain.po.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 系统用户支持类
 *
 * @author samphin
 * @date 2021/1/4 10:57
 */
@Component
public class SystemUserSupport {
    @Resource
    private RedisTemplate<String, SysUser> redisTemplate;

    /**
     * 获取当前登录用户
     *
     * @author samphin
     * @date 2020-12-9 10:05:57
     */
    public String getCurrentUserId() {
        return getCurrentUser().getId();
    }


    /**
     * 获取当前登录用户
     *
     * @author samphin
<<<<<<< HEAD:scene-commons/src/main/java/com/jmyd/commons/base/support/SystemUserSupport.java
     * @since 2020-12-9 10:05:57
=======
     * @date 2021-3-5 14:31:27
>>>>>>> origin/dev:scene-core/src/main/java/com/jmyd/scene/core/system/support/SystemUserSupport.java
     */
    public SysUser getCurrentUser() {
        //每次访问授权时都会将当前用户信息缓存到shiro中
        return (SysUser) SecurityUtils.getSubject().getPrincipal();
    }

    /**
<<<<<<< HEAD:scene-commons/src/main/java/com/jmyd/commons/base/support/SystemUserSupport.java
     * 获取当前请求token
=======
     * 判断当前用户是否可以编辑展厅
     *
     * @author samphin
     * @date 2021-3-5 14:31:27
>>>>>>> origin/dev:scene-core/src/main/java/com/jmyd/scene/core/system/support/SystemUserSupport.java
     */
    public String getAccessToken() {
        return Objects.toString(SecurityUtils.getSubject().getSession(false).getAttribute(JwtConstant.AUTHORIZATION));
    }

    /**
     * 获取请求头开发者用户ID
     *
     * @param request 请求对象
     * @author samphin
<<<<<<< HEAD:scene-commons/src/main/java/com/jmyd/commons/base/support/SystemUserSupport.java
     * @since 2021-7-13 09:30:01
=======
     * @date 2020-12-9 10:05:57
>>>>>>> origin/dev:scene-core/src/main/java/com/jmyd/scene/core/system/support/SystemUserSupport.java
     */
    public String getUserId(HttpServletRequest request) {
        return Objects.toString(request.getSession().getAttribute(Constant.USER_ID));
    }

    /**
     * 获取请求头开发者用户ID
     *
     * @param request 请求对象
     * @author samphin
<<<<<<< HEAD:scene-commons/src/main/java/com/jmyd/commons/base/support/SystemUserSupport.java
     * @since 2021-7-13 09:30:01
=======
     * @date 2020-12-9 10:05:57
>>>>>>> origin/dev:scene-core/src/main/java/com/jmyd/scene/core/system/support/SystemUserSupport.java
     */
    public SysUser getUser(HttpServletRequest request) {
        return (SysUser) request.getSession().getAttribute(Constant.USER);
    }

    /**
     * 校验密码
     * 1、长度不小于6位
     * 2、必须以字母开头
     * 3、必须包含特殊字符
     * 4、必须包含数字
     *
     * @param password 密码
     * @author samphin
     * @date 2021-3-18 10:09:57
     */
    public boolean validatePassword(String password) {
        if (StringUtils.isEmpty(password)) {
            return false;
        }
        if (password.length() < 6) {
            return false;
        }
        if (password.matches("^[a-zA-z](.*)") && password.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)") && password.matches("(.*)\\d+(.*)")) {
            return true;
        }
        return false;
    }

    /**
     * 随机生成10位随机密码
     *
     * @author samphin
     * @date 2021-3-18 10:09:57
     */
    public static String generatePassword() {
        String[] ens = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
                "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringJoiner stringJoiner = new StringJoiner("");
        for (int i = 0; i < 8; i++) {
            stringJoiner.add(ens[(BigDecimal.valueOf(Math.random() * ens.length).intValue())]);
        }
        //String[] specialCharacter = {"`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "]", "{", "}", "\\", "/", "?", ",", ".", "<", ">"};
        //特殊字符集
        String[] specialCharacter = {"~", "!", "@", "#", "$", "%", "&", "*", "-", "_", "?"};
        stringJoiner.add(specialCharacter[(BigDecimal.valueOf(Math.random() * specialCharacter.length).intValue())]);
        stringJoiner.add(String.valueOf(BigDecimal.valueOf(Math.random() * 100).intValue()));
        return stringJoiner.toString();
    }
}
